<?php
class Locale extends CApplicationComponent{
	public $defaultLanuage = array();
	public function init(){
		parent::init();
	}
	public function __construct(){
		$sql="SELECT * FROM languages WHERE `status`=1 AND `default`=1";
		$command=Yii::app()->db->createCommand($sql);
		//$command->bindParam(":status",1,PDO::PARAM_STR);
		// replace the placeholder ":email" with the actual email value
		//$command->bindParam(":default",1,PDO::PARAM_STR);
		if($row = $command->queryRow()){
			$this->defaultLanuage = $row;
		}
	}
	
	public function languageList(){
		$sql="SELECT * FROM languages WHERE `status`=1";
		$command=Yii::app()->db->createCommand($sql);
		//$command->bindParam(":status",1,PDO::PARAM_INT);
		$languageReader = $command->query();
		$languages = array();
		while(($row=$languageReader->read())!==false) {
			//die(print_r(Yii::app()->request));
			$currentUrl = Yii::app()->request->pathInfo;
			if(Yii::app()->urlManager->urlFormat == 'path'){
				$currentUrl = explode('/',$currentUrl);
				unset($currentUrl[0]);
				$path = Yii::app()->request->baseUrl.'/'.@implode('/',array_merge(array($row['code']),$currentUrl));
			}else{
				if(isset($_GET['lang'])){
					$path = 'index.php?'.str_replace('lang='.Yii::app()->language, 'lang='.$row['code'], Yii::app()->request->queryString);
				}else if(empty(Yii::app()->request->queryString)){
					$path = 'index.php?lang='.$row['code'];
				}
			}
			$row['path'] = $path; 
			$languages[] = $row;
		}
		return $languages;
	}
	
}